<?php
/** 
 * MapGuide extension
 * @author FirstStrike
 * @version 1.0
 * @link http://firststrike.net
 */

/**
 * Exit if called outside of MediaWiki
 */
if (!defined('MEDIAWIKI')) die();

$wgExtensionCredits['specialpage'][] = array(
	'name' => 'MapGuide',
	'version' => '0.1',
	'author' => 'FirstStrike',
	'url' => 'http://firststrike.net',
	'description' => 'Adds map browsing.',
	'descriptionmsg' => 'mapguide-desc',
);

// Register class and localisations
$dir = dirname(__FILE__) . '/';
//$wgExtensionMessagesFiles['MapGuide'] = $dir . '/MapGuide.i18n.php';
$wgAutoloadClasses['MapGuide'] = $dir . 'MapGuide.class.php';

//Avoid unstubbing $wgParser on setHook() too early on modern (1.12+) MW versions, as per r35980
if ( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) {
	$wgHooks['ParserFirstCallInit'][] = 'wfMapParserInit';
	$wgHooks['ParserAfterTidy'][] = 'wfDisplayPOIs';
	$wgHooks['SkinTemplateTabs'][] = 'wfAddTabs';
} else { // Otherwise do things the old fashioned way
	$wgExtensionFunctions[] = 'wfMapParserInit';
}

//Default variables
$city = "Washington, DC";
$mapServices = array( "ww" => "Open Layers",
					"gmaps" => "Google Maps",
					"osm" => "OpenStreetMap" );
$mapGuide = new MapGuide();

function wfMapParserInit() {
	global $wgParser;
	$wgParser->setHook( 'geo', 'wfMapRender' );
  $wgParser->setHook( 'poi', 'wfRenderPOI' );	
	return true;
}
 
function wfMapRender( $input, $args, $parser ) {
	global $wgOut, $wgScriptPath, $mapGuide;
	
	$mapGuide->setCity($args[city]);	
//	$mapGuide->setMapServices($mapServices);
	
	$wgOut->addScript( "<script type='text/javascript'>var mode='" . $args[mode] . "'; var city='" . $args[city] . "';var lat=" . $args[lat] . ";var long=" . $args[long] . ";var zoom =" . $args[zoom] . ";</script>");
	$wgOut->addScript( "<script type='text/javascript' src='http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAKFBLYpKnQK-DzIyeU0RnpBSq44a5cAEW7fjZWBBDzJ2qGJ1FLxQmPw4wpnOeKmwRRSPL_wkror8oCQ'></script>" );
  $wgOut->addScript( "<script type='text/javascript' src='" . $wgScriptPath . "/extensions/MapGuide/mapiconmaker.js'></script>" );
	$wgOut->addScript( "<script type='text/javascript' src='" . $wgScriptPath . "/extensions/MapGuide/OpenLayers/OpenLayers.js'></script>" );
  $wgOut->addScript( "<script type='text/javascript' src='" . $wgScriptPath . "/extensions/MapGuide/map.js'></script>" );
	$wgOut->addLink( array('rel'   => 'stylesheet','type'  => 'text/css','href'  => $wgScriptPath . '/extensions/MapGuide/map.css'));	
	
	$text = $mapGuide->setupMap($args[mode]);
	$wgOut->addHTML($text);

	return htmlspecialchars( $input );
}

function wfRenderPOI( $input, $args, $parser ) {
	global $wgOut, $mapGuide;
	$text = $args["name"] . " (" . $args["address"] . ") <a href='javascript:showAddress(\"" . $args["address"] . ", " . $mapGuide->getCity()  . "\", \"" . $args["type"] . "\");'>Map</a> - " . $input;
	$mapGuide->addAddress($args["address"]);
	return $text;
}

function wfDisplayPOIs(&$parser, &$text) {
  global $mapGuide;
  foreach ($mapGuide->getAddList() as $address) {
    $text .= "<script type='text/javascript'>addList.push('". $address . ", " . $mapGuide->getCity() . "');</script>";
  }
  return true;
}

function wfAddTabs($skin,&$content_actions) {
  			$content_actions['hidemap'] = array(
				'class' => ($action == 'hidemap') ? 'selected' : false,
				'text' => "Hide map",
				'href' => "javascript:hideMap();"
			);
  			$content_actions['geocode'] = array(
				'class' => ($action == 'geocode') ? 'selected' : false,
				'text' => "Geocode",
				'href' => "javascript:showAddressGeocoder();"
			);			
      return true;
}
